package cn.com.haopy.yue.ai.admin.web;

import cn.com.haopy.yue.ai.admin.annotation.RequiresPermissionsDesc;
import cn.com.haopy.yue.ai.comm.service.HumanApiService;
import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.core.validator.Order;
import cn.com.haopy.yue.ai.core.validator.Sort;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskUserHumanSource;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskUserHumanSourceService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @Description: TODO
 * @Author DuTao
 * @Date 2025/4/22 8:46
 */
@RestController
@RequestMapping("/admin/risk/user/human/source")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "数字人队列资源相关")
public class AdminAiRiskHumanSourceController {
    @Autowired
    private AIRiskUserHumanSourceService humanSourceService;

    @Autowired
    private HumanApiService humanApiService;

    @RequiresPermissions("admin:risk:user:human:source:list")
    @RequiresPermissionsDesc(menu = {"队列管理", "资源列表"}, button = "资源列表")
    @GetMapping("/list")
    @ApiOperation("列表")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @ApiParam("是否结束") Boolean isFinish,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        Page<AIRiskUserHumanSource> sourceList = humanSourceService.querySelective(page, limit, isFinish, sort, order);
        return ResponseUtil.okPage(sourceList);
    }

    @RequiresPermissions("admin:risk:user:human:source:endUse")
    @RequiresPermissionsDesc(menu = {"队列管理", "资源列表"}, button = "关闭单个资源")
    @GetMapping("/endUse")
    @ApiOperation("结束所有")
    public Object endUse(@RequestParam @ApiParam("签名") String sign,
                         @RequestParam @ApiParam("资源ID") String uuid) throws Exception {
        AIRiskUserHumanSource humanSource = humanSourceService.getBySourceId(uuid, sign);
        if (Objects.nonNull(humanSource)) {
            humanSource.setFinishTime(LocalDateTime.now());
            humanSource.setIsFinish(Boolean.TRUE);
            humanSource.setIsForceFinish(Boolean.TRUE);
            humanSourceService.updateById(humanSource);
        }
        return humanApiService.sessionStop(sign, uuid);
    }

    @RequiresPermissions("admin:risk:user:human:source:endAll")
    @RequiresPermissionsDesc(menu = {"队列管理", "资源列表"}, button = "关闭所有占用资源")
    @GetMapping("/endAll")
    @ApiOperation("结束所有")
    public Object endAll(@RequestParam @ApiParam("签名") String sign) throws Exception {
        humanSourceService.endUseAll();
        return humanApiService.distroyCallSessionsByAppId(sign);
    }
}
